package com.tibco.notepad.ui;

import java.util.List;

import com.tibco.notepad.R;
import com.tibco.notepad.Service.PreferencesUtil;
import com.tibco.notepad.adapter.NotepadAdapter;
import com.tibco.notepad.domain.Notepad;
import com.tibco.notepad.util.FileUtil;
//git.oschina.net/tangjian/Notepad.git
import android.app.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.RelativeLayout;

public class MainActivity extends Activity {
	private ListView mListView;
	private RelativeLayout mRelativeLayout;
	private NotepadAdapter mNotepadAdapter;
	private PreferencesUtil mPreferenceUtil;
	private WriteNewNoteActivity mWriteNewNoteActivity;
	private FileUtil mFileUitl;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mFileUitl = new FileUtil(this);
		init();
		// 注册长按弹出上下文菜单。
		registerForContextMenu(mListView);
	}

	private void init() {
		mRelativeLayout = (RelativeLayout) findViewById(R.id.item);
		mListView = (ListView) findViewById(R.id.listview);
		mNotepadAdapter = new NotepadAdapter(this, getData());
		View view = LayoutInflater.from(this).inflate(R.layout.empty_view_info,
				null);
		mListView.setEmptyView(view);
		mListView.setAdapter(mNotepadAdapter);
		// 设置Listview为空时
		boolean isShow = mNotepadAdapter.isEmpty();
		if (isShow) {
			Log.d("empty", "empty view");
		}
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				List<Notepad> list = mNotepadAdapter.getData();
				if (null != list && list.size() > 0) {
					Intent intent = new Intent();
					intent.setClass(getApplicationContext(),
							WriteNewNoteActivity.class);
					
					mFileUitl.saveListToXml(list);
					List<Notepad> data = mFileUitl.getNotepadList();
					Notepad notepad = data.get(position);
					startActivityForResult(intent, 1);
				}
			}
		});
	}

	/**
	 * 创建anctionBar中的Item
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.action_menu, menu);
		return true;
	}

	/**
	 * anctionBar中New按钮的点击事件。
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_add_item:
			Intent intent = new Intent();
			intent.setClass(getApplicationContext(), WriteNewNoteActivity.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivityForResult(intent, 1);

		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * adapter适配的数据源
	 * 
	 * @return
	 */
	public List<Notepad> getData() {
		return mFileUitl.getNotepadList();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if ((requestCode == 1) && (resultCode == 10)) {
			mNotepadAdapter.setData(getData());
		}
	}

	/**
	 * 创建上下文菜单
	 */
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
		menu.setHeaderTitle("菜单");
		menu.setHeaderIcon(R.drawable.ic_launcher);
		getMenuInflater().inflate(R.menu.context_menu, menu);

	}

	/**
	 * 上下文菜单选中的回调方法
	 */
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
				.getMenuInfo();
		switch (item.getItemId()) {
		case R.id.menu_item_delete:
			List<Notepad> list = mNotepadAdapter.getData();
			Notepad notepad = list.get(info.position);
			list.remove(notepad);
			mFileUitl.saveListToXml(list);
			mFileUitl.getNotepadList();
			mNotepadAdapter.notifyDataSetChanged();
			break;
		}

		return super.onContextItemSelected(item);
	}

}
